package com.atguigu.stgy.app.service.impl;

import com.atguigu.stgy.app.mapper.BrowsingHistoryMapper;
import com.atguigu.stgy.app.service.*;
import com.atguigu.stgy.common.interceptor.StgyAuthInterceptor;
import com.atguigu.stgy.model.dto.RoomInfoSearchDTO;
import com.atguigu.stgy.model.dto.RoomListSearchDTO;
import com.atguigu.stgy.model.entity.*;
import com.atguigu.stgy.model.vo.ApartmentDetailVO;
import com.atguigu.stgy.model.vo.RoomDetailVO;
import com.atguigu.stgy.model.vo.RoomInfoVO;
import com.atguigu.stgy.model.vo.RoomListVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.atguigu.stgy.model.entity.RoomInfo;
import com.atguigu.stgy.app.mapper.RoomInfoMapper;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements RoomInfoService {
    @Resource
    GraphInfoService graphInfoService;
    @Resource
    RoomAttrValueService roomAttrValueService;
    @Resource
    LeaseAgreementService leaseAgreementService;
    @Resource
    RoomFacilityService roomFacilityService;
    @Resource
    RoomPaymentTypeService roomPaymentTypeService;
    @Resource
    LeaseTermService leaseTermService;
    @Resource
    LabelInfoService labelInfoService;
    @Resource
    ApartmentInfoService apartmentInfoService;

    @Resource
    BrowsingHistoryService browsingHistoryService;

    @Override
    public Page<RoomInfoVO> pageItem(RoomInfoSearchDTO searchDTO) {
        //RoomInfoVO的数据来自于多张表 需要多表联查
        //自定义sql查询
        Page<RoomInfoVO> page = new Page<>(searchDTO.getCurrent() , searchDTO.getSize());
        List<RoomInfoVO>  vos = baseMapper.selectPageItem(page ,searchDTO );
        page.setRecords(vos);
        return page;
    }
//    @Async
    @Override
    public RoomDetailVO getDetailById(Long id) {
        //1、查询roominfo的数据
        RoomInfo roomInfo = this.getById(id);
        if(roomInfo==null){
            return null;
        }
        //房间的签约状态如果是 1/2 表示已入住 否则未入住
        //1.2 查询房间的入住信息  如果roomid对应的房间 存在签约信息 并且状态为1，2 表示房间已入住
        long count = leaseAgreementService.count(Wrappers.lambdaQuery(LeaseAgreement.class)
                .eq(LeaseAgreement::getRoomId, id)
                .in(LeaseAgreement::getStatus, 1, 2));
        // 之前签约的房间 如果租期时间到了 如何更新房间的签约信息的状态  定时任务
        //2、查询房间的图片列表: roomid
        List<GraphInfo> graphInfos = graphInfoService.list(Wrappers.lambdaQuery(GraphInfo.class)
                .eq(GraphInfo::getItemId, id));

        //3、查询房间的属性和值列表
        List<AttrValue> attrValues = roomAttrValueService.selectAttrValueVosByRoomId(id);
        //4、查询房间的配套信息列表
        List<FacilityInfo> facilityInfoList = roomFacilityService.selectListByRoomId(id);
        //5、查询房间的支付方式列表
        List<PaymentType> paymentTypes = roomPaymentTypeService.selectListByRoomId(id);
        //6、查询房间的租期列表
        List<LeaseTerm> leaseTermList  = leaseTermService.selectListByRoomId(id);
        //7、查询label标签列表
        List<LabelInfo> labelInfoList = labelInfoService.selectListByRoomId(id);
        //8、查询公寓详情
        ApartmentDetailVO apartmentDetailVO = apartmentInfoService.getDetailById(roomInfo.getApartmentId());


        //组装VO数据
        RoomDetailVO roomDetailVO = new RoomDetailVO();
        //1、room_info的数据
        BeanUtils.copyProperties(roomInfo , roomDetailVO);
        roomDetailVO.setIsDelete(roomInfo.getDeleted());
        roomDetailVO.setIsCheckIn(count>0);//roominfo表中不存在该字段  需要我们自己查询
        //2、图片列表
        roomDetailVO.setGraphVoList(graphInfos);
        //3、属性和值列表
        roomDetailVO.setAttrValueVoList(attrValues);
        //4、配套列表
        roomDetailVO.setFacilityInfoList(facilityInfoList);
        //5、支付方式列表
        roomDetailVO.setPaymentTypeList(paymentTypes);
        //6、租期列表
        roomDetailVO.setLeaseTermList(leaseTermList);
        //7、房间的标签列表
        roomDetailVO.setLabelInfoList(labelInfoList);
        //8、公寓详情
        roomDetailVO.setApartmentItemVo(apartmentDetailVO);
        //9、公寓杂费列表
        roomDetailVO.setFeeValueVoList(apartmentDetailVO.getFeeValueVoList());


        //保存浏览记录: 和上面的主业务不具有逻辑上的原子性  可以异步执行
        // spring context提供的Async异步任务框架 执行异步任务
        // 1、启动类启用异步任务  2、异步任务方法加上async注解
        BrowsingHistory browsingHistory = new BrowsingHistory();
        browsingHistory.setRoomId(id);
        browsingHistory.setUserId(StgyAuthInterceptor.THREAD_LOCAL.get());
        browsingHistory.setBrowseTime(new Date());
        browsingHistoryService.saveAsync(browsingHistory);

        return roomDetailVO;
    }
    //根据公寓id查询可用房间列表
    @Override
    public Page<RoomListVO> pageItemByApartmentId(RoomListSearchDTO searchDTO) {
        Page<RoomListVO> page = new Page<>(searchDTO.getCurrent() , searchDTO.getSize());
        List<RoomListVO> list = baseMapper.pageItemByApartmentId(page,searchDTO);
        page.setRecords(list);
        return page;
    }

    @Override
    public BigDecimal getMinRentByApartmentId(Long apartmentId) {

        return baseMapper.selectMinRentByApartmentId(apartmentId);
    }
}
